package socket

import (
	"fmt"
	"net"
)

type TCPClient struct {
	ip       string
	port     int
	conn     net.Conn
	callback func()
}

func (t *TCPClient) Connect(ip string, port int) {
	t.ip, t.port = ip, port
	addr := fmt.Sprintf("%s:%d", t.ip, t.port)
	con, e := net.Dial("tcp", addr)
	if e != nil {
		panic(e)
	}
	t.conn = con
}

func (t *TCPClient) Send(message string) bool {
	_, e := t.conn.Write([]byte(message))
	return e == nil
}

func (t *TCPClient) Recv(length int) string {
	buf := make([]byte, length)
	_, e := t.conn.Read(buf)
	if e != nil {
		panic(e)
	}
	value := string(buf)
	fmt.Printf("==%s==\n", value)
	return value
}
